package gui.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.beans.PropertyChangeListener;

import javax.swing.JPanel;

/**
 * 
 * @author Felix Schliephacke
 * 
 */
class SouthPanel extends JPanel {

	private static final long serialVersionUID = 5619225803780949436L;
	private ClearButton clearButton;
	private NextSolutionButton nextSolutionButton;
	private JPanel northButtonPanel;
	private RestoreButton restoreButton;
	private SolveButton solveButton;
	private JPanel southButtonPanel;

	public SouthPanel() {
		this.setLayout(new BorderLayout());
		this.add(this.getNorthButtonPanel(), BorderLayout.NORTH);
		this.add(this.getSouthButtonPanel(), BorderLayout.SOUTH);
	}

	@Override
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		this.getClearButton().addPropertyChangeListener(listener);
		this.getSolveButton().addPropertyChangeListener(listener);
		this.getNextSolutionButton().addPropertyChangeListener(listener);
		this.getRestoreButton().addPropertyChangeListener(listener);
	}

	@Override
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		this.getClearButton().removePropertyChangeListener(listener);
		this.getSolveButton().removePropertyChangeListener(listener);
		this.getNextSolutionButton().removePropertyChangeListener(listener);
		this.getRestoreButton().removePropertyChangeListener(listener);
	}

	private ClearButton getClearButton() {
		if (this.clearButton == null) {
			this.clearButton = new ClearButton("Clear");
		}
		return this.clearButton;
	}

	private NextSolutionButton getNextSolutionButton() {
		if (this.nextSolutionButton == null) {
			this.nextSolutionButton = new NextSolutionButton("next Solution");
		}
		return this.nextSolutionButton;
	}

	private Component getNorthButtonPanel() {
		if (this.northButtonPanel == null) {
			this.northButtonPanel = new JPanel();
			this.northButtonPanel.setLayout(new FlowLayout());
			this.northButtonPanel.add(this.getSolveButton());
			this.northButtonPanel.add(this.getClearButton());
		}
		return this.northButtonPanel;
	}

	private RestoreButton getRestoreButton() {
		if (this.restoreButton == null) {
			this.restoreButton = new RestoreButton("Restore");
		}
		return this.restoreButton;
	}

	private SolveButton getSolveButton() {
		if (this.solveButton == null) {
			this.solveButton = new SolveButton("Solve");
		}
		return this.solveButton;
	}

	private Component getSouthButtonPanel() {
		if (this.southButtonPanel == null) {
			this.southButtonPanel = new JPanel();
			this.southButtonPanel.setLayout(new FlowLayout());
			this.southButtonPanel.add(this.getNextSolutionButton());
			this.southButtonPanel.add(this.getRestoreButton());
		}
		return this.southButtonPanel;
	}
}
